package com.sunfeng.n4.deadlock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

public class Test31 {

    private static Thread t1;

    private static Thread t2;
    private static Thread t3;

    public static void main(String[] args) {

        ParkUnPark parkUnPark = new ParkUnPark(5);


         t1 = new Thread(() -> {
            parkUnPark.print("a", t2);
        }, "t1");


         t2 = new Thread(() -> {
            parkUnPark.print("b", t3);
        }, "t2");


         t3 = new Thread(() -> {
            parkUnPark.print("c", t1);
        }, "t3");
        t1.start();
        t2.start();
        t3.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LockSupport.unpark(t1);

    }
}


class ParkUnPark {

    public ParkUnPark(int loopNUmber) {
        this.loopNUmber = loopNUmber;
    }

    private int loopNUmber;


    public void print(String str, Thread next) {
        for (int i = 0; i < loopNUmber; i++) {
            LockSupport.park();
            System.out.println(str);
            LockSupport.unpark(next);
        }

    }

}
